<?php

namespace api\controller;
// +----------------------------------------------------------------------
// | 实体控制层
// +----------------------------------------------------------------------
use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\ApsService;
use common\service\AssetService;
use common\service\AsyncCallService;
use common\service\CommonService;
use common\service\EntityGenerateService;
use common\service\EntityRelationService;
use common\service\EntityService;
use common\service\EntityUseService;
use common\service\SequenceService;
use common\service\ShotService;
use common\service\StatusService;
use common\service\StoryboardService;
use common\validate\Entity;
use support\ErrorCode;
use think\Exception;
use think\module\Module;
use think\Request;
use think\Response;
use Throwable;
use Webman\Stomp\Client;

class EntityController extends VerifyController
{
    /**
     * @var EntityService
     */
    protected $entityService;
    private $assetService;
    private $shotService;
    private $asyncCallService;
    private $apsService;
    private $statusService;
    private $storyboardService;
    private $sequenceService;
    private $entityUseService;
    /** @var EntityRelationService */
    private $entityRelationService;
    /** @var EntityGenerateService */
    private $entityGenerateService;

    public function __construct()
    {
        parent::__construct();
        $this->entityService = EntityService::getInstance();
        $this->assetService = AssetService::getInstance();
        $this->shotService = ShotService::getInstance();
        $this->asyncCallService = AsyncCallService::getInstance();
        $this->apsService = new ApsService();
        $this->statusService = StatusService::getInstance();
        $this->storyboardService = StoryboardService::getInstance();
        $this->sequenceService = SequenceService::getInstance();
        $this->entityUseService = EntityUseService::getInstance();
        $this->entityRelationService = EntityRelationService::getInstance();
        $this->entityGenerateService = EntityGenerateService::getInstance();
    }

    /**
     * 实体自动排期
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function autoScheduling(Request $request)
    {
        $param = $request->param();
        $resData = $this->apsService->autoSchedulingAsync($param['param']);
        return json(success_response('', $resData));
    }

    /**
     * 生成指定项目的实体任务
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function generateEntityStepCategoryTask(Request $request)
    {
        $param = $request->param();
        $entityIds = $param['entity_ids'] ?? '';
        $entityIds = implode(',', $entityIds);
        $resData = $this->asyncCallService->generateEntityStepCategoryTask($param['project_id'], $param['module_code'], $entityIds);
        return json(success_response('', $resData));
    }

    /**
     * 创建实体
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function sampleCreate(Request $request)
    {
        $validate = new Entity();
        switch ($request->getModuleCode()) {
            case "design":
                $validate->scene("CreateDesign");
                break;
            case "asset":
                $validate->scene("CreateAsset");
                break;
            case "level":
                $validate->scene("CreateLevel");
                break;
            case "shot":
                $validate->scene("CreateShot");
                break;
            case "episode":
                $validate->scene("CreateEpisode");
                break;
            case "sequence":
                $validate->scene("CreateSequence");
                break;
            default:
                $validate->scene("Create");
        }
        $param = $request->param();
        if (!$validate->check($param)) {
            throw_strack_exception($validate->getError(), ErrorCode::ENTITY_CREATE_PARAM);
        }
        try {
            $res = $this->entityService->sampleCreate($param, Module::$moduleDictData['module_index_by_code'][$request->getModuleCode()], $request->getTenantId());
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除实体
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function delete(Request $request)
    {
        $param = $request->param();
        $res = $this->entityService->delete($param);
        return json(success_response('', $res));
    }

    /**
     * 自动生成清单之间关联关系
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function autoEntityListRelate(Request $request)
    {
        $param = $request->param();
        $res = $this->entityService->autoRelateDesign($param['project_id']);
        return json(success_response('', $res));
    }

    /**
     * 通过概念生成资产和关卡
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function generateAssetAndLevelByDesign(Request $request)
    {
        $filter = [];
        $param = $request->param();
        if (!empty($param['param']['filter'])) {
            $filter = $param['param']['filter'];
        }
        try {
            $res = EntityGenerateService::getInstance()->generateAssetAndLevelByDesign($param['project_id'], $request->getTenantId(), $filter, 'task_generated');
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 通过分镜数据生成镜头
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function generateShotByStoryboard(Request $request)
    {
        $filter = [];
        $param = $request->param();
        if (!empty($param['param']['filter'])) {
            $filter = $param['param']['filter'];
        }
        try {
            $res = EntityGenerateService::getInstance()->generateShotByStoryboard($param['project_id'], $request->getTenantId(), $filter, 'task_generated');
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 自动转换分镜里的关卡和资产关联文字
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function autoTranslateAssetAndLevel(Request $request): Response
    {
        $filter = [];
        $param = $request->param();
        if (!empty($param['param']['filter'])) {
            $filter = $param['param']['filter'];
        }
        $res = $this->entityService->autoTranslateAssetAndLevel($param['project_id'], $request->getTenantId(), $filter);
        return json(success_response('', $res));
    }

    /**
     * 编剧提交
     * @param Request $request
     * @return Response
     * @throws Exception|\Exception
     */
    public function submit(Request $request)
    {
        $res = $this->entityService->submit($request->param()['param']['filter'], $request->getModuleCode());
        return json(success_response('', $res));
    }

    /**
     * 发布实体 (生成任务)
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function publish(Request $request)
    {
        try {
            $res = $this->entityService->publish($request->param()['param']['filter'], $request->getModuleCode(), $request->getTenantId());
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 发布实体 (生成任务V3)
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function publishV3(Request $request)
    {
        try {
            $res = $this->entityService->publishV3($request->param()['param']['filter'], $request->getModuleCode(), $request->getTenantId());
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 资产和关卡发布后检查关联信息
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function designPublishCheckRelated(Request $request)
    {
        try {
            $res = $this->entityGenerateService->designPublishCheckRelated($request->param()['param']['filter']['id']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 组装序列
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function assemblySequence(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->entityService->assemblySequence($param['param']['filter'], $param['data']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 更新序列
     * @param Request $request
     * @return Response
     */
    public function updateSequence(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->entityService->updateSequence($param['data']);
            return json(success_response('', $res));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 更新实体媒体信息
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function updateEntityMediaInfo(Request $request)
    {
        $param = $request->param();
        $res = $this->entityService->updateEntityMediaInfo($param['data'], $request->getModuleCode());
        return json(success_response('', $res));
    }

    /**
     * 状态列表
     * @param Request $request
     * @return Response
     */
    public function statusList(Request $request)
    {
        $res = $this->statusService->entityStatusList($request->getModuleCode(), $request->param()['param']['sub_module_code']);
        return json(success_response('OK', $res));
    }

    /**
     * 需求状态列表
     * @return Response
     */
    public function demandStatusList()
    {
        $res = $this->statusService->entityDemandStatusList();
        return json(success_response('OK', $res));
    }

    /**
     * 同步关联数据
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function syncRelationData(Request $request)
    {
        $res = $this->entityRelationService->syncRelationData($request->param()['param']['filter'], $request->getModuleCode());
        return json(success_response('OK', $res));
    }

    /**
     * 重写实体更新
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function update(Request $request)
    {
        /**
         * 监听 概念更新
         */
        $param = $request->param();
        if ($request->getModuleCode() === 'design') {
            $param['data'] = $this->entityService->designStatusAutoChange($param['data']);
        }
        $instance = CommonService::instance($request->controller());
        $instance->setCurrentModuleCode($request->moduleCode);
        $throwNoDataUpdateException = $request->param()['throw_no_data_change_exception'] ?? 'yes';
        $throwNoDataUpdateException = $throwNoDataUpdateException === 'yes';
        $resData = $instance->commonUpdate($param, Module::$moduleDictData['module_index_by_code'][$request->moduleCode], $throwNoDataUpdateException);
        return json(success_response('', $resData));
    }

    /**
     * 批量导入实体
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function import(Request $request)
    {
        $dataList = $request->param()['data_list'];
        $validate = new Entity();
        $validate->scene('ImportItem');
        foreach ($dataList as $data) {
            if (!$validate->check($data)) {
                throw_strack_exception("data_list array item " . $validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
        }
        $res = $this->entityService->import($request->param()['data_list'], $request->getModuleCode());
        return json(success_response('OK', $res));
    }

    /**
     * 移除组序
     * @param Request $request
     * @return Response
     * @throws  \Exception
     */
    public function removeSequence(Request $request)
    {
        $res = $this->entityService->removeSequence($request->param()['param'], $request->getTenantId());
        return json(success_response('OK', $res));
    }

    /**
     * 查询实体列表
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getList(Request $request)
    {
        $res = $this->entityService->getList($request->param()['param'], $request->getModuleCode());
        return json(success_response('OK', $res));
    }

    /**
     * 剧本导入
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function scriptImport(Request $request)
    {
        $dataList = $request->param()['data_list'];
        $validate = new Entity();
        $validate->scene('ScriptImportItem');
        foreach ($dataList as $data) {
            if (!$validate->check($data)) {
                throw_strack_exception("data_list array item " . $validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
        }
        $res = $this->entityService->scriptImport($request->param()['data_list'], $request->getModuleCode());
        return json(success_response('OK', $res));
    }

    /**
     * 根据类型更新概念资产关联关卡
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function updateDesignRelation(Request $request)
    {
        $res = $this->entityRelationService->byTypeUpdateDesignRelation($request->param()['data'], $request->getModuleCode());
        return json(success_response('OK', $res));
    }

    /**
     * 修改并重新生成任务
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function modifyAndRegenerateTask(Request $request)
    {
        try {
            $res = $this->entityService->modifyAndRegenerateTask($request->param()['data'], $request->getModuleCode(), $request->getTenantId());
            return json(success_response('OK', $res));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 分镜序列列表
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function getStoryboardSequenceList(Request $request)
    {
        $res = $this->entityService->getStoryboardSequenceList($request->param()['param'], $request->getTenantId(), $request->getModuleCode());
        return json(success_response('OK', $res));
    }

    /**
     * 资产合并模块实体任务树
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getAssetMergeModuleEntityTaskTree(Request $request)
    {
        try {
            $res = $this->assetService->getMergeModuleEntityTaskTree($request->param()['param'], $request->getTenantId(), $request->getModuleCode());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 序列合并模块实体任务树
     * @param Request $request
     * @return Response|void
     * @throws \Exception
     */
    public function getSequenceMergeModuleEntityTaskTree(Request $request)
    {
        try {
            $res = $this->shotService->getShotMergeModuleEntityTaskTree($request->param()['param'], $request->getTenantId());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 镜头合并模块实体任务树
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getShotEntityTaskTree(Request $request)
    {
        try {
            $res = $this->assetService->getMergeModuleEntityTaskTree($request->param()['param'], $request->getTenantId(), $request->getModuleCode());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 分镜序列树
     * @param Request $request
     * @return Response
     */
    public function getStoryboardSequenceTree(Request $request)
    {
        $res = $this->entityService->getStoryboardSequenceTree($request->param()['param'], $request->getModuleCode());
        return json(success_response('OK', $res));
    }

    /**
     * 分配任务弹出任务树查询
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getAssignPopTaskTree(Request $request)
    {
        try {
            $filter = $request->param()['param']['filter'];
            $res = $this->shotService->getAssignPopTaskTree($filter['task_ids']);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改并重新生成序列任务
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function modifySequenceTask(Request $request)
    {
        try {
            $res = $this->entityService->modifySequenceTask($request->param()['data']);
            return json(success_response('OK', $res));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    public function getSequenceFilter(Request $request)
    {
        try {
            $res = $this->entityService->getSequenceFilter($request->param()['param']['filter']);
            return json(success_response('OK', $res));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 批量更新数据
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function batchUpdate(Request $request)
    {
        $dataList = $request->param()['data_list'];
        $validate = new Entity();
        $validate->scene('BatchUpdateItem');
        foreach ($dataList as $data) {
            if (!$validate->check($data)) {
                throw_strack_exception("data_list array item " . $validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
        }
        try {
            $res = $this->entityService->batchUpdate($request->param()['data_list'], $request->getModuleCode());
            return json(success_response('OK', $res));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 同步剧本的场次
     * @param Request $request
     * @return Response
     */
    public function syncSceneToScript(Request $request)
    {
        $res = $this->entityService->syncSceneToScript();
        return json(success_response('OK', $res));
    }

    public function syncListToScript(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->entityService->syncListToScript();
        return json(success_response('OK', $res));
    }

    /**
     * 同步集关系到资产
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function syncEpisodeRelationToAsset(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->entityService->syncEpisodeRelationToAsset($param);
        return json(success_response('OK', $res));
    }

    /**
     * 资产搜索推荐
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function assetSearchRecommend(Request $request)
    {
        $filter = $request->param()['param']['filter'];
        $limit = $request->param()['param']['limit'] ?? 10;
        $res = $this->assetService->searchRecommend($filter, $limit);
        return json(success_response('OK', $res));
    }

    /**
     * 获取生成的实体任务列表
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getGenerateEntityTaskList(Request $request)
    {
        $params = $request->param()['param'] ?? [];
        $res = $this->assetService->getGenerateEntityTaskList($params, $request->getTenantId(), $request->getUserId());
        return json(success_response('OK', $res));
    }

    /**
     * 组装动捕序列
     * @param Request $request
     * @return Response|void
     * @throws Throwable
     */
    public function assembleMotionSequence(Request $request)
    {
        $data = $request->param()['data'];
        try {
            $res = $this->sequenceService->assembleMotionSequence($data['storyboard_id'], $data['sequence_data'], $request->getTenantId());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 动捕序列列表
     * @param Request $request
     * @return Response|void
     * @throws \Exception
     */
    public function motionSequenceList(Request $request)
    {
        $param = $request->param()['param'];
        $param['page'] = $param['page'] ?? [1, C("database.database_max_select_rows")];
        $param['filter'] = $param['filter'] ?? [];
        $param['filter']['sequence.tenant_id'] = $request->getTenantId();
        try {
            $res = $this->sequenceService->motionSequenceList($param);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 动捕序列详情
     * @param Request $request
     * @return Response|void
     * @throws \Exception
     */
    public function motionSequence(Request $request)
    {
        $param = $request->param()['param'];
        $param['filter'] = $param['filter'] ?? [];
        $param['filter']['sequence.tenant_id'] = $request->getTenantId();
        $param['fields'] = $param['fields'] ?? '';
        try {
            $res = $this->sequenceService->motionSequence($param);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取最大序列号
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getMaxSequenceName(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $res = $this->entityService->getMaxSequenceName($param);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改镜头工序
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function modifyShotEntityStep(Request $request)
    {
        $param = $request->param()['data'];
        try {
            $res = $this->entityService->modifyShotEntityStep($param);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 创建实体待办任务
     * @param Request $request
     * @return Response|void
     * @throws Throwable
     */
    public function createTodoTask(Request $request)
    {
        $data = $request->param()['data'];
        try {
            $res = $this->entityService->createTodoTask($data, $request->getUserId(), $request->getTenantId());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除实体待办任务
     * @param Request $request
     * @return Response|void
     * @throws Exception
     * @throws Throwable
     */
    public function deleteTodoTask(Request $request)
    {
        $todoTaskId = $request->param()['param']['id'];
        try {
            $res = $this->entityService->deleteTodoTask($todoTaskId);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 弃用实体待办任务
     * @param Request $request
     * @return Response|void
     * @throws Exception
     * @throws Throwable
     */
    public function deprecateTodoTask(Request $request)
    {
        $todoTaskId = $request->param()['param']['id'];
        try {
            $res = $this->entityService->deprecateTodoTask($todoTaskId, $request->getUserId());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 启用实体待办任务
     * @param Request $request
     * @return Response|void
     * @throws Exception
     * @throws Throwable
     */
    public function enableTodoTask(Request $request)
    {
        $todoTaskId = $request->param()['param']['id'];
        try {
            $res = $this->entityService->enableTodoTask($todoTaskId, $request->getUserId());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 创建实体待办任务
     * @param Request $request
     * @return Response|void
     * @throws Exception
     * @throws Throwable
     */
    public function batchCreateTodoTask(Request $request)
    {
        $dataList = $request->param()['data_list'];
        $validate = new Entity();
        $validate->scene('BatchCreateTodoTaskItem');
        foreach ($dataList as $data) {
            if (!$validate->check($data)) {
                throw_strack_exception("data_list array item " . $validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
        }
        try {
            $res = $this->entityService->batchCreateTodoTask($dataList, $request->getUserId(), $request->getTenantId());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 关联资产贴图待办
     * @param Request $request
     * @return Response|void
     * @throws Exception
     */
    public function relateTodoTask(Request $request)
    {
        try {
            $storyboardIdList = $request->param()['data']['storyboard_id_list'];
            $entityId = $request->param()['data']['entity_id'];
            $todoTaskId = $request->param()['data']['todo_task_id'];
            $res = $this->storyboardService->relateTodoTask($storyboardIdList, $entityId, $todoTaskId);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 通过实体查找所属的工序
     * @param Request $request
     * @return Response
     */
    public function findEntityStepList(Request $request)
    {
        $param = $request->param()["param"];
        $res = $this->entityService->findEntityStepList($param["filter"]["entity_id"]);
        return json(success_response('OK', $res));
    }

    /**
     * 通过实体查找对应的工序任务进度
     * @param Request $request
     * @return Response
     */
    public function findEntityStepTaskSchedule(Request $request)
    {
        $param = $request->param()["param"];
        $res = $this->entityService->findEntityStepTaskSchedule($param["filter"], $request->getModuleCode());
        return json(success_response('OK', $res));
    }

    /**
     * 通过实体查找对应的动捕序列任务
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function findMotionSequenceTask(Request $request)
    {
        try {
            $param = $request->param()["param"];
            $res = $this->sequenceService->findMotionSequenceTask($param["filter"]["entity_id"]);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 搜索剧集实体工序列表
     * @param Request $request
     * @return Response
     */
    public function searchEpisodeEntityStepList(Request $request)
    {
        $param = $request->param();
        if (empty($param["param"]["filter"]["entity.project_id"])) {
            throw_strack_exception("entity.project_id is not empty", ErrorCode::REQUEST_PARAM_ERROR);
        }
        $res = $this->entityService->searchEpisodeEntityStepList($param["param"], $param["module_code"]);
        return json(success_response('OK', $res));
    }

    /**
     * 查询一个未知类型的实体
     * @param Request $request
     * @return Response
     */
    public function getUnknownEntity(Request $request)
    {
        $param = $request->param();
        $res = $this->entityService->getUnknownEntity($param["param"]);
        return json(success_response('OK', $res));
    }

    /**
     * 设置剧集封板和播出
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function setEpisodeSealOrShow(Request $request)
    {
        $param = $request->param();
        $res = $this->entityService->setEpisodeSealOrShow($request->getModuleCode(), $param["data"]['type'], $param['data']);
        return json(success_response('OK', $res));
    }

    /**
     * 获取实体关联情况
     * @param Request $request
     * @return Response
     */
    public function getUseInfo(Request $request)
    {
        $param = $request->param()['param'];
        $entityIds = $param['entity_ids'] ?? '';
        $resData = $this->entityUseService->getUseInfo($entityIds);
        return json(success_response('', $resData));
    }

    /**
     * 获取动捕序列分镜清单
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function getMotionSequenceStoryboardList(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->sequenceService->getMotionSequenceStoryboardList($param);
        return json(success_response('', $resData));
    }

    /**
     * 保存动捕序列
     * @throws Throwable
     * @throws Exception
     */
    public function saveMotionSequence(Request $request)
    {
        $data = $request->param()['data'];
        $validate = new Entity();
        $validate->scene('CreateMotionSequence');
        if (empty($data['sequence_data']['id']) && !$validate->check($data)) {
            throw_strack_exception($validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
        }
        $validate->scene('UpdateMotionSequence');
        if (!empty($data['sequence_data']['id']) && !$validate->check($data)) {
            throw_strack_exception($validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
        }
        $resData = $this->sequenceService->saveMotionSequence($data['storyboard_id'] ?? null, $data['sequence_data'], $request->getTenantId(), $request->getUserId());
        return json(success_response('', $resData));
    }

    /**
     * 移除动捕序列分镜
     * @param Request $request
     * @return Response
     * @throws Throwable
     * @throws Exception
     */
    public function revokeMotionSequenceStoryboard(Request $request)
    {
        $filter = $request->param()['param']['filter'];
        $resData = $this->sequenceService->revokeMotionSequenceStoryboard($filter['storyboard_ids'], $request->getTenantId(), $request->getUserId());
        return json(success_response('', $resData));
    }

    /**
     * 获取序列下的分镜资产
     * @param Request $request
     * @return Response
     */
    public function findSequenceStoryboardAsset(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->sequenceService->findSequenceStoryboardAsset($param);
        return json(success_response('', $resData));
    }

    /**
     * 获取实体清单任务进度列表
     * @param Request $request
     * @return Response
     */
    public function getEntityStepTaskScheduleList(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->entityService->getEntityStepTaskScheduleList($param['filter'], $request->getModuleCode());
        return json(success_response('', $resData));
    }

    /**
     * 获通过实体查找对应的动捕序列任务列表
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function getMotionSequenceTaskList(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->sequenceService->getMotionSequenceTaskList($param["filter"]["entity_id"]);
        return json(success_response('', $resData));
    }

    /**
     * 获取资产关联的镜头列表
     * @param Request $request
     * @return Response
     */
    public function findAssetRelationShotList(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->entityService->findAssetRelationShotList($param["filter"]["entity_id"]);
        return json(success_response('', $resData));
    }

    /**
     * 任务卡片-查询资产
     * @param Request $request
     * @return Response
     */
    public function getRelationAsset(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->entityService->getRelationAsset($param, $request->getModuleCode());
        return json(success_response('', $resData));
    }

    /**
     * 实体状态检查
     *
     * @param Request $request
     * @return Response
     */
    public function statusCheck(Request $request)
    {
        $param = $request->param()['param'];
        $async = $param['is_async'] ?? 'no';
        if ($async === 'yes') {
            // 触发实体关联检查队列
            $resData = Client::send('check_entity_status', build_queue_data_with_xu(['entity_id' => $param['id']]));
        } else {
            $resData = $this->entityService->statusCheck($param['id']);
        }
        return json(success_response('', $resData));
    }

    /**
     * 同步关联关系到relationships表
     * @param Request $request
     * @return Response
     */
    public function syncRelationShips(Request $request)
    {
        $id = $request->param()['param']['id'];
        AsyncCallService::getInstance()->syncRelationDataToEntityRelationShipsAsync($id);
        return json(success_response('', 1));
    }


    /**
     * 获取实体默认属性
     * @param Request $request
     * @return Response|void
     */
    public function getEntityDefaultAttrValue(Request $request)
    {
        try {
            $attrList = $request->param()['param']['attr_list'] ?? '';
            $res = $this->entityService->getEntityDefaultAttrValue($attrList, $request->getModuleCode());
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取异常实体列表环节状态数据
     * @param Request $request
     * @return Response|void
     */
    public function getAbnormalEntityStepCategoryList(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $entityIds = $param['entity_ids'] ?? [];
            $res = $this->entityService->getAbnormalEntityStepCategoryList($entityIds);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 获取异常任务延期确认数据
     * @param Request $request
     * @return Response|void
     */
    public function getAbnormalEntityDelayConfirmList(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $taskIds = $param['task_ids'] ?? [];
            $deliveryTime = $param['delivery_time'] ?? [];
            $res = $this->entityService->getAbnormalEntityDelayConfirmList( $deliveryTime, $taskIds);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }
}
